﻿<%@ WebHandler Language="C#" Class="list" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Web.Script.Serialization;
using System.Web.SessionState;
using System.Web.Services;
using System.IO;
using System.Drawing;


public class list : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
    private StringBuilder sb = null;
    public DataSet ds = null;
    public DataTable dt = null;
    private string sqlwhere = "";
    public void ProcessRequest (HttpContext context) {
        string type = PageBase.ext_string(context.Request.Params["type"]);
        switch (type)
        {
            //查询类型下拉选项
            case "getselect":
                getselect(context);
                break;
            //查询上传图片信息
            case "getlist":
                getinfo_list(context);
                break;
            //新增
            case "add":
                add(context);
                break;
           //修改
            case "edit":
                edit(context);
                break;
            //删除
            case "delete":
                delete(context);
                break;
            //根据id获取信息
            case "getlist_byid":
                getlist_byid(context);
                break;
            default:
                //getinfo(context);
                break;
        }
    }
    //查询图标根据id
    public void getlist_byid(HttpContext context)
    {
        string editid = PageBase.ext_string(context.Request.Params["editid"]);
        string sql = @"select id, typeid, name, describe, imgurl, xh, ltrim(rtrim(isactive)) isactive, createtime
                      from [t_pic] where id={0}";
        sql = string.Format(sql,editid);
        DataTable dt = DBHelper.ExecuteDataTable(sql);

        string fsmessage = "";
        sb = new StringBuilder();
        string code = "0";
        string msg = "";

        sb.Append("{\"code\":\"" + code + "\",\"msg\":\"" + msg + "\",\"data\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            string urlgsh = "../../" + dt.Rows[i]["imgurl"];
            sb.Append("{\"id\":\"" + dt.Rows[i]["id"].ToString() + "\"");
            sb.Append(",\"typeid\":\"" + dt.Rows[i]["typeid"] + "\"");
            sb.Append(",\"name\":\"" + dt.Rows[i]["name"] + "\"");
            sb.Append(",\"describe\":\"" + dt.Rows[i]["describe"] + "\"");
            sb.Append(",\"imgurl\":\"" + urlgsh + "\"");
            sb.Append(",\"xh\":\"" + dt.Rows[i]["xh"] + "\"");
            sb.Append(",\"isactive\":\"" + dt.Rows[i]["isactive"] + "\"");
            sb.Append(",\"createtime\":\"" + dt.Rows[i]["createtime"] + "\"}");
            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //删除
    public void delete(HttpContext context)
    {
        string getids = PageBase.ext_string(context.Request.Params["getids"]);
        string msg = "";
        bool flag;
        string sqldelete = "update  t_pic set isactive='N' where id in ({0})";
        sqldelete = string.Format(sqldelete, getids);
        int dtcount= DBHelper.ExecuteNonQuery(sqldelete);
        if (dtcount > 0)
        {
            flag = true;
            msg = "删除成功";
        }
        else
        {
            flag = false;
            msg = "删除失败";

        }
        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }

    //查询
    public void getinfo_list(HttpContext context)
    {
        //页数1
        int page = PageBase.ext_int(context.Request.Params["page"]);
        //每页条数10
        int limit =PageBase.ext_int(context.Request.Params["limit"]);
        //排序字段
        string field =PageBase.ext_string(context.Request.Params["field"]);
        //排序方式
        string order = PageBase.ext_string(context.Request.Params["order"]);

        string orderby = "createtime";
        string where = @"select  COUNT(*) OVER(PARTITION BY 1) AS TOTAL,b.typename,a.id,a.name,a.describe,a.imgurl,a.xh,a.isactive,a.createtime from 
                       [dbo].[t_pic] a left join [t_type] b on a.typeid=b.id where a.isactive='Y'
                       and 1=1 ";
        string typeid =PageBase.ext_string(context.Request.Params["typeid"]);
        string txt_name =PageBase.ext_string(context.Request.Params["txt_name"]);
                    string txt_bh =PageBase.ext_string(context.Request.Params["txt_bh"]);
                    //类型
        if (!string.IsNullOrEmpty(typeid))
        {
            where += " and a.typeid = '" + typeid + "'";
        }
        //名称
        if (!string.IsNullOrEmpty(txt_name))
        {
            where += " and a.name like N'%" + txt_name + "%'";
        }
        //编号
        if (!string.IsNullOrEmpty(txt_bh))
        {
            where += " and a.describe like N'%" + txt_bh + "%'";
        }
        if (!string.IsNullOrEmpty(order) && !string.IsNullOrEmpty(field))
        {
            orderby =field + " " + order;
        }
        DataTable dt =  DBHelper.Pagination2(where, limit, page, orderby, "");
        sb = new StringBuilder();
        string code = "0";
        string msg = "";
      int count = 0;
        if(dt.Rows.Count>0)
            count = PageBase.ext_int(dt.Rows[0]["TOTAL"].ToString());
        sb.Append("{\"code\":\"" + code + "\",\"count\":" + count + ",\"msg\":\"" + msg + "\",\"data\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            string urlgsh = "../../" + dt.Rows[i]["imgurl"];
            sb.Append("{\"id\":\"" + dt.Rows[i]["id"].ToString() + "\"");
            sb.Append(",\"typename\":\"" + dt.Rows[i]["typename"] + "\"");
            sb.Append(",\"name\":\"" + dt.Rows[i]["name"] + "\"");
            sb.Append(",\"describe\":\"" + dt.Rows[i]["describe"] + "\"");
            sb.Append(",\"imgurl\":\"" + urlgsh + "\"");
            sb.Append(",\"xh\":\"" + dt.Rows[i]["xh"] + "\"");
            sb.Append(",\"isactive\":\"" + dt.Rows[i]["isactive"] + "\"");
            sb.Append(",\"createtime\":\"" + dt.Rows[i]["createtime"] + "\"}");
            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //查询类型下拉选项
    public void getselect(HttpContext context)
    {
        string ysdid = PageBase.ext_string(context.Request.Params["ysdid"]);
        string sql = @"select * from [t_type] where isactive='Y'";
        DataTable dt = DBHelper.ExecuteDataTable(sql);

        string fsmessage = "";
        sb = new StringBuilder();
        string code = "0";
        string msg = "";
        sb.Append("{\"code\":\"" + code + "\",\"msg\":\"" + msg + "\",\"data\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            sb.Append("{\"id\":\"" + dt.Rows[i]["id"].ToString() + "\"");
            sb.Append(",\"typename\":\"" + dt.Rows[i]["typename"] + "\"");
            sb.Append(",\"remark\":\"" + dt.Rows[i]["remark"] + "\"");
            sb.Append(",\"isactive\":\"" + dt.Rows[i]["isactive"] + "\"");
            sb.Append(",\"createtime\":\"" + dt.Rows[i]["createtime"] + "\"}");
            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //修改
    public void edit(HttpContext context)
    {
        string msg = "";
        string sqlsave = "";
        bool flag;
        GetDate list = new GetDate();
        using (System.IO.StreamReader sr = new System.IO.StreamReader(context.Request.InputStream))
        {
            string s = sr.ReadToEnd();
            list = new JavaScriptSerializer().Deserialize<GetDate>(s);
        }
        string id = Utility.Trim(list.LoadData_type[0].id);
        string lx = Utility.Trim(list.LoadData_type[0].lx);
        string txt_name = Utility.Trim(list.LoadData_type[0].txt_name);
        string txt_bm = Utility.Trim(list.LoadData_type[0].txt_bm);
        string txt_xh = Utility.Trim(list.LoadData_type[0].txt_xh);
        string imgurl = Utility.Trim(list.LoadData_type[0].imgurl);
        string isactive = Utility.Trim(list.LoadData_type[0].ISACTIVE);
        string isactive_= isactive == "True" ? "Y" : "N";

        string ishave = "select * from t_pic where name='{0}' and typeid='{1}' isactive='Y'";
        ishave = string.Format(ishave, txt_name,lx);

        DataTable dtcount= DBHelper.ExecuteDataTable(ishave);
        if (dtcount.Rows.Count > 0 && dtcount.Rows[0]["id"].ToString() != id)
        {
            flag = false;
            msg = "已存在此名称，请修改";

        }
        else
        {
            sqlsave = @"update [t_pic] set 
                                   [typeid]='{0}'
                                  ,[name]='{1}'
                                  ,[describe]='{2}'
                                  ,[imgurl]='{3}'
                                  ,[xh]='{4}'
                                  ,[isactive]='{5}'
                                  ,[modifytime]='{6}'
                                   where id={7};";
            sqlsave = string.Format(sqlsave, lx, txt_name, txt_bm, imgurl, txt_xh, isactive_, DateTime.Now.ToString(), id);
            int zxcount = DBHelper.ExecuteNonQuery(sqlsave);
            if (zxcount > 0)
            {
                flag = true;
                msg = "修改成功";
            }
            else
            {
                flag = false;
                msg = "修改失败，请联系管理员";
            }
        }
        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //新增
    public void add(HttpContext context)
    {
        string msg = "";
        string sqlsave = "";
        bool flag;
        GetDate list = new GetDate();
        using (System.IO.StreamReader sr = new System.IO.StreamReader(context.Request.InputStream))
        {
            string s = sr.ReadToEnd();
            list = new JavaScriptSerializer().Deserialize<GetDate>(s);
        }
        string lx = Utility.Trim(list.LoadData_type[0].lx);
        string txt_name = Utility.Trim(list.LoadData_type[0].txt_name);
        string txt_bm = Utility.Trim(list.LoadData_type[0].txt_bm);
        string txt_xh = Utility.Trim(list.LoadData_type[0].txt_xh);
        string imgurl = Utility.Trim(list.LoadData_type[0].imgurl);
        string isactive = Utility.Trim(list.LoadData_type[0].ISACTIVE);
        string isactive_= isactive == "True" ? "Y" : "N";

        string ishave = "select * from t_pic where name='{0}' and typeid='{1}' isactive='Y'";
        ishave = string.Format(ishave, txt_name,lx);
        DataTable dtcount= DBHelper.ExecuteDataTable(ishave);
        if (dtcount.Rows.Count > 0)
        {
            flag = false;
            msg = "已存在此名称，请修改";
        }
        else
        {
            sqlsave = @"INSERT INTO [t_pic]
                                   ([typeid]
                                   ,[name]
                                   ,[describe]
                                   ,[imgurl]
                                   ,[xh]
                                   ,[isactive]
                                   ,[createtime]
                                   )
                             VALUES
                                   ('{0}'
                                   ,'{1}'
                                   ,'{2}'
                                   ,'{3}'
                                   ,'{4}'
                                   ,'{5}'
                                   ,'{6}'
                                  );";
            sqlsave = string.Format(sqlsave, lx, txt_name,txt_bm,imgurl,txt_xh,isactive_, DateTime.Now.ToString());
            int zxcount = DBHelper.ExecuteNonQuery(sqlsave);
            if (zxcount > 0)
            {
                flag = true;
                msg = "新增成功";
            }
            else
            {
                flag = false;
                msg = "新增失败，请联系管理员";
            }
        }
        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    public class GetDate
    {
        public List<typemanager> LoadData_type { get; set; }
    }
    public class typemanager
    {
        //id
        public string id;
        public string ID
        {
            get { return id; }
            set { id = value; }
        }
        //类型名称
        public string lx;
        public string LX
        {
            get { return lx; }
            set { lx = value; }
        }
        public string txt_name;
        public string TXT_NAME
        {
            get { return txt_name; }
            set { txt_name = value; }
        }
        public string txt_bm;
        public string TXT_BM
        {
            get { return txt_bm; }
            set { txt_bm = value; }
        }
        public string imgurl;
        public string IMGURL
        {
            get { return imgurl; }
            set { imgurl = value; }
        }
        public int txt_xh;
        public int TXT_XH
        {
            get { return txt_xh; }
            set { txt_xh = value; }
        }
        public string isactive;
        public string ISACTIVE
        {
            get { return isactive; }
            set { isactive = value; }
        }
    }
    public bool IsReusable {
        get {
            return false;
        }
    }

}